package com.top75;

import com.bean.TreeNode;

/**
 * @Author: huangzhigao
 * @Date: 2023/5/23 20:11
 */
public class Code437 {

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return 0;
        }
        int ret = dfs(root, targetSum);
        ret += pathSum(root.left, targetSum);
        ret += pathSum(root.right, targetSum);
        return ret;

    }

    public int dfs(TreeNode node, int targetSum) {
        int ret = 0;

        if (node == null) {
            return 0;
        }
        int val = node.val;
        if (val == targetSum) {
            ret++;
        }

        ret += dfs(node.left, targetSum - node.val);
        ret += dfs(node.right, targetSum - node.val);
        return ret;
    }


}
